package com.gopay.common.cps.dao.fund.impl;

import com.gopay.common.base.dao.impl.BaseDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.cps.dao.fund.BindCardDAO;
import com.gopay.common.domain.fund.FundGopayCardEntity;
import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @BindCardDAOImpl.java
 * @author baiyunpeng
 * @2016年7月4日 上午10:27:00  www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("bindCardDAO")
public class BindCardDAOImpl extends BaseDAOImpl<FundGopayCardEntity, String>  implements BindCardDAO {

    @Override
    public FundGopayCardEntity save(FundGopayCardEntity entity) {
        if(entity!=null){
            super.save(entity);
            return entity;
        }
       return null;
    }
    @Override
    public FundGopayCardEntity getCard(String agentId) {
        if(agentId!=null && !"".equals(agentId)) {
            StringBuffer sql = new StringBuffer("" +
                    "from FundGopayCardEntity where agentSignNo=:agentId");
            Map<String,Object>  map = new HashMap<>();
            map.put("agentId",agentId);
            List<FundGopayCardEntity> byHql = findByHql(sql.toString(), map);
            if(byHql!=null && byHql.size()>0){
                return byHql.get(0);
            }
        }
        return null;
    }
    @Override
    public FundGopayCardEntity getValidCard(String agentId) {
        if(agentId!=null && !"".equals(agentId)) {
            StringBuffer sql = new StringBuffer("" +
                    "from FundGopayCardEntity where agentSignNo=:agentId and type='1'");
            Map<String,Object>  map = new HashMap<>();
            map.put("agentId",agentId);
            List<FundGopayCardEntity> byHql = findByHql(sql.toString(), map);
            if(byHql!=null && byHql.size()>0){
                return byHql.get(0);
            }
        }
        return null;
    }

    @Override
    public FundGopayCardEntity getValidCard(String agentId, String merId) {
        if(StringUtils.isNotEmpty(agentId) && StringUtils.isNotEmpty(merId)) {
            StringBuffer sql = new StringBuffer("" +
                    "from FundGopayCardEntity where agentSignNo=:agentId and merchantId=:merId and type='1'");
            Map<String,Object>  map = new HashMap<>();
            map.put("agentId",agentId);
            map.put("merId",merId);
            List<FundGopayCardEntity> byHql = findByHql(sql.toString(), map);
            if(byHql!=null && byHql.size()>0){
                return byHql.get(0);
            }
        }
        return null;
    }

    @Override
    public FundGopayCardEntity findByCriteria(String bankNo) {
        DetachedCriteria deCriteria = DetachedCriteria.forClass(FundGopayCardEntity.class);
        if(StringUtils.isNotBlank(bankNo)){
            deCriteria.add(Restrictions.eq("bankNo",bankNo));
        }
        List<FundGopayCardEntity> list = getHibernateTemplate().findByCriteria(deCriteria);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    //本方法没有被使用过，如果有调用需要，应该针对多卡情况做改造
    @Deprecated
    @Override
    public List<FundGopayCardEntity> getCardByAgentOrMerOrderId(String agentNo, String merOrderId) {
        StringBuffer sql = new StringBuffer("");
        sql.append("from FundGopayCardEntity where agentSignNo=:agentSignNo or merOrderNum=:merOrderNum");
        if(agentNo!=null && merOrderId!=null) {
            Map<String,Object> map = new HashMap<>();
            map.put("agentSignNo",agentNo);
            map.put("merOrderNum",merOrderId);
           return findByHql(sql.toString(),map);
        }
        return null;
    }
    @Override
    public void deleteCard(FundGopayCardEntity card) {
        if(card!=null){
            delete(card);
        }

    }
   @Override
   public void updateCard(FundGopayCardEntity fundGopayCardEntity){
        StringBuffer sql = new StringBuffer("");
       if(fundGopayCardEntity!=null) {
           sql.append("UPDATE FundGopayCardEntity set type='2' , lastUpdTs=:lastUpdTs where agentSignNo=:agentSignNo AND merchantId=:merchantId");
           Map<String, Object> map = new HashMap<>();
           map.put("agentSignNo", fundGopayCardEntity.getAgentSignNo());
           map.put("merchantId", fundGopayCardEntity.getMerchantId());
           map.put("lastUpdTs", new Date());
           //add by liuyu, 20180102, 活期宝基金支付支持多卡改造，增加卡号查询条件-----start----->
           if(StringUtils.isNotBlank(fundGopayCardEntity.getBankNo())){
                sql.append(" and bankNo in (:cardNoList)");
                map.put("cardNoList", SensitiveInfoUtils.getAllHisCipherArray(fundGopayCardEntity.getBankNo()));
           }
           //add by liuyu, 20180102, 活期宝基金支付支持多卡改造，增加卡号查询条件------end----->

           updateByHql(sql.toString(),map);
       }
    }

    @Override
    public FundGopayCardEntity getValidCardByAgentAndCardNo(String agentId, String cardNo) {
        if(StringUtils.isNotBlank(agentId) && StringUtils.isNotBlank(cardNo)) {
            String hql = "from FundGopayCardEntity where bankNo in (:cardNo) and agentSignNo=:agentId and type='1'";
            Map<String,Object>  map = new HashMap<>();
            map.put("agentId",agentId);
            map.put("cardNo",SensitiveInfoUtils.getAllHisCipherArray(cardNo));
            List<FundGopayCardEntity> byHql = findByHql(hql, map);
            if(byHql!=null && byHql.size()>0){
                return byHql.get(0);
            }
        }
        return null;
    }

    @Override
    public FundGopayCardEntity getValidCardByAgentAndMerIdAndCardNo(String agentId, String merId, String cardNo) {
        if(StringUtils.isNotBlank(agentId) && StringUtils.isNotBlank(merId) && StringUtils.isNotBlank(cardNo)) {
            String hql = "from FundGopayCardEntity where bankNo in (:cardNo) and agentSignNo=:agentId and merchantId=:merId and type='1'";
            Map<String,Object>  map = new HashMap<>();
            map.put("agentId",agentId);
            map.put("merId",merId);
            map.put("cardNo",SensitiveInfoUtils.getAllHisCipherArray(cardNo));
            List<FundGopayCardEntity> byHql = findByHql(hql, map);
            if(byHql!=null && byHql.size()>0){
                return byHql.get(0);
            }
        }
        return null;
    }
}
